/**   
 * @Title: EditTextPreference.java 
 * @Package com.jiuyang.baoxian.widget 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author ChangJianlong   
 * @date 2014-12-16 下午3:52:43 
 * @version V1.0   
 */
package com.bxd.baodan.widget;

import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.BasePreferenceActivity;
import com.bxd.baodan.activity.ChooseCityActivity;
import com.bxd.baodan.activity.ModifyPasswordActivity;
import com.bxd.baodan.activity.ModifyPhoneActivity;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.app.SharedPreferanceKey;
import com.bxd.baodan.db.DBHelper;
import com.bxd.baodan.item.UserItem;
import com.bxd.baodan.util.DialogUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.NetUtil.RequestStringListener;
import com.bxd.baodan.util.SpUtil;
import com.bxd.baodan.widget.InsureHelperListDialog.ItemClickListener;

/**
 * @ClassName: EditTextPreference
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author ChangJianlong
 * @date 2014-12-16 下午3:52:43
 * 
 */
public class EditTextPreference extends android.preference.EditTextPreference {
	private TextView value;

	public EditTextPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/*
	 * (非 Javadoc) <p>Title: onBindDialogView</p> <p>Description: </p>
	 * 
	 * @param view
	 * 
	 * @see
	 * android.preference.EditTextPreference#onBindDialogView(android.view.View)
	 */
	@Override
	protected void onBindDialogView(View view) {
		// TODO Auto-generated method stub
		super.onBindDialogView(view);
		this.getEditText().setText(
				SpUtil.getInstance().getString(this.getKey()));
	}

	/*
	 * (非 Javadoc) <p>Title: onCreateView</p> <p>Description: </p>
	 * 
	 * @param parent
	 * 
	 * @return
	 * 
	 * @see android.preference.Preference#onCreateView(android.view.ViewGroup)
	 */
	@Override
	protected View onCreateView(ViewGroup parent) {
		// TODO Auto-generated method stub
		return LayoutInflater.from(getContext()).inflate(
				R.layout.layout_for_edit_preference, parent, false);
	}

	/*
	 * (非 Javadoc) <p>Title: onBindView</p> <p>Description: </p>
	 * 
	 * @param view
	 * 
	 * @see android.preference.Preference#onBindView(android.view.View)
	 */
	@Override
	protected void onBindView(View view) {
		super.onBindView(view);
		value = (TextView) view.findViewById(R.id.value);
		if (SharedPreferanceKey.NICK_NAME.equals(this.getKey())) {
			value.setText(SpUtil.getInstance().getString(
					SharedPreferanceKey.NICK_NAME));
		} else if (SharedPreferanceKey.ACCOUNT.equals(this.getKey())) {
			value.setText(SpUtil.getInstance().getString(
					SharedPreferanceKey.ACCOUNT));
		} else if (SharedPreferanceKey.QQ.equals(this.getKey())) {
			value.setText(SpUtil.getInstance()
					.getString(SharedPreferanceKey.QQ));
		} else if (SharedPreferanceKey.AREA.equals(this.getKey())) {
			if (LoginInfo.getInstance(getContext()).getUser().getCity() != null
					&& !LoginInfo.getInstance(getContext()).getUser().getCity()
							.equals("")) {
				String sheng="";
				try {
					 sheng=DBHelper.getInstance(getContext()).getProvince(
							Integer.parseInt(LoginInfo.getInstance(getContext())
									.getUser().getUe_prncode())).getName();
				} catch (Exception e) {
					// TODO: handle exception
				}
				String shi="";
				try {
					shi=DBHelper.getInstance(getContext()).getCity(
							Integer.parseInt(LoginInfo
									.getInstance(getContext()).getUser()
									.getUe_citycode())).getName();
				} catch (Exception e) {
					// TODO: handle exception
				}
				value.setText(sheng+ " "
						+ shi);
			} else {
				value.setText(SpUtil.getInstance().getString(
						SharedPreferanceKey.AREA));
			}
		} else {
			value.setText(SpUtil.getInstance().getString(this.getKey()));
		}
	}

	/*
	 * (非 Javadoc) <p>Title: onDialogClosed</p> <p>Description: </p>
	 * 
	 * @param positiveResult
	 * 
	 * @see android.preference.EditTextPreference#onDialogClosed(boolean)
	 */
	@Override
	protected void onDialogClosed(boolean positiveResult) {

		// 请求网络成功后调用super

		if (!positiveResult) {
			return;
		}

		if (SharedPreferanceKey.NICK_NAME.equals(this.getKey())) {
			if(!this.getEditText().getEditableText().toString().trim().equals("")){
				editNick(this.getEditText().getEditableText().toString().trim());
			}else{
				DialogUtil.getInstance().showToast(getContext(),
						"昵称不能为空，请重新修改");
			}
		} else if (SharedPreferanceKey.ACCOUNT.equals(this.getKey())) {

		} else if (SharedPreferanceKey.QQ.equals(this.getKey())) {
			editQQ(this.getEditText().getEditableText().toString().trim());

		} else if (SharedPreferanceKey.AREA.equals(this.getKey())) {

		} else {
			super.onDialogClosed(positiveResult);
			value.setText(SpUtil.getInstance().getString(this.getKey()));
		}
	}

	private void startActivity(Class<?> targetClass) {
		((BasePreferenceActivity) (this.getContext()))
				.openActivity(targetClass);
	}

	/*
	 * (非 Javadoc) <p>Title: onClick</p> <p>Description: </p>
	 * 
	 * @see android.preference.DialogPreference#onClick()
	 */
	@Override
	protected void onClick() {
		if (SharedPreferanceKey.ACCOUNT.equals(this.getKey())) {
			showMyDialog();

		} else if (SharedPreferanceKey.AREA.equals(this.getKey())) {
			this.startActivity(ChooseCityActivity.class);
		} else {
			super.onClick();
		}
	}

	private void showMyDialog() {
		String[] sexes = { "更换新手机号", "更换新密码" };

		InsureHelperListDialog ihld = new InsureHelperListDialog(
				this.getContext(), null, sexes, 0, 0);
		ihld.setItemClickListener(new ItemClickListener() {

			@Override
			public void onItemClick(int position) {
				doSelected(position);
			}
		});
		ihld.show();

	}

	protected void doSelected(int position) {
		switch (position) {
		case 0:
			this.startActivity(ModifyPhoneActivity.class);
			break;
		case 1:
			this.startActivity(ModifyPasswordActivity.class);
			break;
		default:
			break;
		}
	}

	/**
	 * 修改昵称
	 */
	protected void editNick(final String nickName) {
		NetUtil netUtil = new NetUtil((PreferenceActivity) this.getContext(),
				JsonApi.MODIFYINFOR);
		netUtil.setParams("ver", JsonApi.VAR);
		netUtil.setParams("uid", LoginInfo.getInstance(getContext()).getUser()
				.getU_id());
		netUtil.setParams("pwd", LoginInfo.getInstance(getContext()).getUser()
				.getPwd());
		netUtil.setParams("nick", nickName);
		netUtil.setParams("test", "bxdwenwen5971");

		netUtil.postRequest("", new RequestStringListener() {

			@Override
			public void onComplete(String result) {
				if (JSONUtil.isSuccess(result)) {
					UserItem item = LoginInfo.getInstance(
							getContext().getApplicationContext()).getUser();
					item.setU_nick(nickName);
					LoginInfo.getInstance(getContext()).saveUserInfo(
							new Gson().toJson(item),
							EditTextPreference.this.getContext());
					EditTextPreference.this.persistString(nickName);
					SpUtil.getInstance().savaString(
							SharedPreferanceKey.NICK_NAME, nickName);
					value.setText(SpUtil.getInstance().getString(
							SharedPreferanceKey.NICK_NAME));
					EditTextPreference.this.getContext().sendBroadcast(
							new Intent(Constant.ACTION_SUCCESS_MODIFIED_INFOR));
				} else {
					DialogUtil.getInstance().showToast(getContext(),
							JSONUtil.getMessage(result));
				}
			}
		});
	}

	protected void editQQ(final String qq) {
		NetUtil netUtil = new NetUtil((PreferenceActivity) this.getContext(),
				JsonApi.MODIFYINFOR);
		netUtil.setParams("ver", JsonApi.VAR);
		netUtil.setParams("uid", LoginInfo.getInstance(getContext()).getUser()
				.getU_id());
		netUtil.setParams("pwd", LoginInfo.getInstance(getContext()).getUser()
				.getPwd());
		netUtil.setParams("qq", qq);
		// netUtil.setParams("province", LoginInfo.getInstance(getAty())
		// .getUser().getUe_prncode() );
		// netUtil.setParams("city", LoginInfo.getInstance(getAty())
		// .getUser().getUe_citycode());
		netUtil.postRequest("", new RequestStringListener() {

			@Override
			public void onComplete(String result) {
				if (JSONUtil.isSuccess(result)) {
					UserItem item = LoginInfo.getInstance(
							getContext().getApplicationContext()).getUser();
					item.setQq(qq);
					LoginInfo.getInstance(getContext()).saveUserInfo(
							new Gson().toJson(item),
							EditTextPreference.this.getContext());
					EditTextPreference.this.persistString(qq);
					value.setText(SpUtil.getInstance().getString(
							SharedPreferanceKey.QQ));
					SpUtil.getInstance().savaString(SharedPreferanceKey.QQ, qq);
					EditTextPreference.this.getContext().sendBroadcast(
							new Intent(Constant.ACTION_SUCCESS_MODIFIED_INFOR));
				} else {
					DialogUtil.getInstance().showToast(getContext(),
							JSONUtil.getMessage(result));
				}
			}
		});
	}
}
